<?php $this->load->view('layout/header'); ?>
<div class="supplier-content-page">
    <div class="product-test-h5">
        <ul class="breadcrumb">
            <li><a href="javascript:;">商品管理</a></li>
            <li><a href="<?php echo site_url('tourismgoodsbase/grid')?>">宝贝管理</a></li>
            <li class="active"><a href="javascript:void(0);">添加宝贝</a></li>
        </ul>
        <?php echo execute_alert_message(); ?>
        <div class="product-view-area"><p>商品添加</p></div>
        <div class="add-form">
            <form class="form-horizontal" action="<?php echo site_url('tourismgoodsbase/addPost') ?>" method="post"
                  id="tourism_goods_add">
                <div class="add-product infomation">
                    <div class="fonter-add">添加商品基本信息</div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label">商品类别</label>
                        <div class="col-xs-10">
                            <input type="text" name="category_name" value="<?php echo $category ?>"  class="form-control required" readonly="readonly"/>
                            <input type="hidden" name="category_id" value="<?php echo $this->input->get('class_c') ?>" class="form-control required" readonly="readonly"/>
                            <input type="hidden" name="is_beizhu" value="2"/>
                        </div>
                    </div>
                    <?php if ($providerCategory->num_rows() > 0) : ?>
                    <div class="form-group">
                        <label class="col-sm-1 control-label"><em>* </em>供应商类别</label>
                        <div class="col-xs-2">
                            <select name="provider_category_id" class="form-control required">
                                <?php foreach ($providerCategory->result() as $item):?>
                                    <option value="<?php echo $item->id ?>">
                                        <?php echo $item->category_name ?>
                                    </option>
                                <?php endforeach;?>
                            </select>
                        </div>
                    </div>
                    <?php endif;?>
                    <div class="form-group">
                        <label class="col-sm-1 control-label"><em>* </em>商品名称</label>
                        <div class="col-xs-10">
                            <input type="text" name="goods_name" class="form-control required" placeholder="商品名称"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label">商品简介</label>
                        <div class="col-xs-10">
                            <textarea name="goods_brief" class="form-control" placeholder="商品简介" ></textarea>
                        </div>
                    </div>
                    <div class="form-group file-images-add">
                        <label class="col-sm-1 control-label">图片</label>
                        <div class="col-xs-10">
                            <div class="file-image">

                            </div>
                            <input type="file" name="images">
                            <input type="hidden" name="images" value="">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label"><em>* </em>商品详情</label>
                        <div class="col-xs-10">
                            <textarea name="wap_goods_desc" class="form-control required textarea-multipart-edit" placeholder="商品详情"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label"><em>* </em>净重（g）</label>
                        <div class="col-xs-10">
                            <input type="text" name="weight" class="form-control number required" placeholder="重量g">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label">限购数量</label>
                        <div class="col-xs-10">
                            <input type="text" name="limit_num" class="form-control number" placeholder="限购数量(0为不限)">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label">限购天数</label>
                        <div class="col-xs-10">
                            <input type="text" name="limit_day" class="form-control number" placeholder="限购天数(0为没有限购天数)，1天为当天以内，30天就是当天往前数30天，即1天内限购多少件，30天内限购多少件！">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label"><em>* </em>是否特产</label>
                        <div class="col-xs-5">
                            <label class="radio-inline">
                                <input type="radio" name="goods_native" value="2" class="required" checked="checked" autocomplete="off"/>是
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="goods_native" value="1" class="required" autocomplete="off"/>否
                            </label>
                        </div>
                    </div>
                    <?php if ($brand->num_rows() > 0) : ?>
                        <div class="form-group">
                            <label class="col-sm-1 control-label"><em>* </em>品牌</label>
                            <div class="col-xs-2">
                                <select name="brand_id" class="form-control required">
                                	<option value="0">无</option>
                                    <?php foreach ($brand->result() as $bd):?>
                                        <option value="<?php echo $bd->brand_id ?>">
                                            <?php echo $bd->brand_name ?>
                                        </option>
                                    <?php endforeach;?>
                                </select>
                            </div>
                        </div>
                    <?php endif; ?>
                    <div class="form-group">
                        <label class="col-sm-1 control-label"><em>* </em>上下架</label>
                        <div class="col-xs-2">
                            <select name="is_on_sale" class="form-control required">
                                <option value="1">上架</option>
                                <option value="2">下架</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="add-product infomation">
                    <div class="fonter-add">商品销售信息</div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label required"><em>* </em>市场售价</label>
                        <div class="col-xs-3 required">
                            <input type="text" name="market_price" class="form-control number required" placeholder="市场售价">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label"><em>* </em>销售价</label>
                        <div class="col-xs-3">
                            <input type="text" name="shop_price" class="form-control number required" placeholder="销售价">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label required"><em>* </em>供应价</label>
                        <div class="col-xs-3">
                            <input type="text" name="supply_price" class="form-control number required" placeholder="供应价">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label"><em>* </em>总库存数</label>
                        <div class="col-xs-3">
                            <input type="text" name="goods_num" class="form-control number required" placeholder="总库存数">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label"><em>* </em>分润模板</label>
                        <div class="col-xs-2">
                            <select name="rate_id" class="form-control required">
                                <?php foreach ($profitRate->result() as $val):?>
                                <option value="<?php echo $val->rate_id;?>"><?php echo $val->name;?></option>
                                <?php endforeach;?>
                            </select>
                        </div>
                    </div>
                </div>
                <?php if ($attribute->num_rows() > 0): ?>
                    <div class="add-product infomation">
                        <div class="fonter-add">商品规格属性</div>
                        <?php foreach ($attribute->result() as $key => $item) : ?>
                            <?php $spec_attr = $this->tourism_goods_category_spec->getSpecByAttId($item->attr_id); ?>
                            <?php if ($spec_attr->num_rows() > 1): ?>
                                <div class="form-group">
                                    <label class="col-sm-1 control-label"><?php echo $item->attr_name ?>：</label>
                                    <div class="col-xs-10">
                                        <?php foreach ($spec_attr->result() as $val) : ?>
                                            <label class="radio-inline">
                                                <input type="checkbox" name="attribute[]" value="<?php echo $val->spec_id ?>"><?php echo $val->attribute_value ?>
                                            </label>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
                <?php if ($additive->num_rows() > 0) : ?>
                    <div class="add-product infomation">
                        <div class="fonter-add">商品附加属性</div>
                        <?php foreach ($additive->result() as $item): ?>
                            <div class="form-group">
                                <label class="col-sm-1 control-label"><?php echo $item->name ?></label>
                                <div class="col-xs-3">
                                    <input type="text" name="value[]" placeholder="<?php echo $item->name ?>" class="form-control" />
                                    <input type="hidden" name="key[]" value="<?php echo $item->name ?>" readonly="readonly" />
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
                <div class="add-product infomation">
                    <div class="fonter-add">商品运费信息</div>
                    <div class="form-group">
                        <label class="col-sm-12 control-label"><em>* </em>发货地</label>
                        <div class="col-xs-1">
                            <select id="cmbProvince" name="province_id" class="form-control required"></select>
                        </div>
                        <div class="col-xs-1">
                            <select id="cmbCity" name="city_id" class="form-control required"></select>
                        </div>
                        <div class="col-xs-1">
                            <select id="cmbArea" name="district_id" class="form-control required"></select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label"><em>* </em>详细地址</label>
                        <div class="col-xs-5">
                            <input type="text" name="detail" class="form-control required" placeholder="详细地址"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label"><em>* </em>支付方式</label>
                        <div class="col-xs-4">
                            <label class="checkbox-inline">
                                <input type="checkbox" value="1" name="payment[]" class="required" checked="checked"/>在线支付
                            </label>
                            <label class="checkbox-inline">
                                <input type="checkbox" value="2" name="payment[]" />货到付款
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label"><em>* </em>邮费设置</label>
                        <div class="col-xs-5">
                            <label class="radio-inline">
                                <input type="radio" name="transport_type" value="0" class="required" checked="checked" autocomplete="off"/>卖家承担运费
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="transport_type" value="1" class="required" autocomplete="off"/>使用运费模板
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="transport_type" value="2" class="required" autocomplete="off"/>自定义运费
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label"></label>
                        <div class="col-xs-3" style="display: none">
                            <select name="transport_id" class="form-control" style="display:none">
                                <?php foreach ($transport->result() as $item): ?>
                                    <option value="<?php echo $item->id ?>"><?php echo $item->name ?></option>
                                <?php endforeach; ?>
                            </select>
                            <input type="text" name="transport_cost" value="0" class="form-control number " style="display: none" />
                        </div>
                    </div>
                </div>
                <div class="sup-submit">
                    <button type="submit" class="btn btn-default">保存</button>
                    <a href="<?php echo site_url('tourismgoodsbase/addstep1'); ?>">返回上一级</a>
                </div>
            </form>
        </div>
    </div>
</div>
<?php $this->load->view('layout/footer'); ?>
<script type="text/javascript">
    //CKedit 编辑器 js
    if ($('textarea.textarea-multipart-edit').size() > 0) {
        KindEditor.create('textarea.textarea-multipart-edit', {  
            width : '100%',
            height :'400px'
       });
    }
    $("input[name='transport_type']").click(function () {
        var k = $(this).val();
        var c = parseInt(k);
        var t = $(this).parents('div .form-group').next();
        t.find("div").show();
        if (c == 1) {
            t.find("select").show().addClass('required');
            t.find("input").val(0).hide().removeClass('required');
        }
        if (c == 2) {
            t.find("select").hide().removeClass('required');;
            t.find("input").show().addClass('required');
        }
        if (c != 1 && c != 2) {
            t.find("select").hide().removeClass('required');;
            t.find("input").val(0).hide().removeClass('required');;
        }
    });

    var dataInit = function () {
        var provinceList = '';
        $.ajax({
            type: "GET",
            url: hostUrl() + "/region/select",
            dataType: 'json',
            async: false,
            success: function (d) {
                provinceList = d;
            }
        });
        return provinceList;
    }
    var provinceList = dataInit();
    var addressInit = function (_cmbProvince, _cmbCity, _cmbArea, defaultProvince, defaultCity, defaultArea) {
        var cmbProvince = document.getElementById(_cmbProvince);
        var cmbCity = document.getElementById(_cmbCity);
        var cmbArea = document.getElementById(_cmbArea);

        function cmbSelect(cmb, str) {
            for (var i = 0; i < cmb.options.length; i++) {
                if (cmb.options[i].value == str) {
                    cmb.selectedIndex = i;
                    return;
                }
            }
        }

        function cmbAddOption(cmb, str, val) {
            var option = document.createElement("OPTION");
            cmb.options.add(option);
            option.innerHTML = str;
            option.value = val;
        }

        function changeCity() {
            cmbArea.options.length = 0;
            if (cmbCity.selectedIndex == -1)return;
            var item = cmbCity.options[cmbCity.selectedIndex].value;
            //provinceList = dataInit(item);
            for (var i = 0; i < provinceList.length; i++) {
                if (provinceList[i].parent_id == item) {
                    cmbAddOption(cmbArea, provinceList[i].region_name, provinceList[i].region_id);
                }
            }
            cmbSelect(cmbArea, defaultArea);
        }

        function changeProvince() {
            cmbCity.options.length = 0;
            cmbCity.onchange = null;
            if (cmbProvince.selectedIndex == -1)return;
            var item = cmbProvince.options[cmbProvince.selectedIndex].value;
            //provinceList = dataInit(item);
            for (var i = 0; i < provinceList.length; i++) {
                if (provinceList[i].parent_id == item) {
                    cmbAddOption(cmbCity, provinceList[i].region_name, provinceList[i].region_id);
                }
            }
            cmbSelect(cmbCity, defaultCity);
            changeCity();
            cmbCity.onchange = changeCity;
        }

        //初始化省级
        //provinceList = dataInit(1);
        for (var i = 0; i < provinceList.length; i++) {
            if (provinceList[i].parent_id == 1) {
                cmbAddOption(cmbProvince, provinceList[i].region_name, provinceList[i].region_id);
            }
        }
        cmbSelect(cmbProvince, defaultProvince);
        changeProvince();
        cmbProvince.onchange = changeProvince;
    }
    addressInit('cmbProvince', 'cmbCity', 'cmbArea');

    $('#tourism_goods_add').validate({ignore:''});
    
    $('button.btn-info').click(function () {
        if (!$('#tourism_goods_add').valid()) {
            return false;
        }
    });

    $('.form-horizontal').on('change', 'input[type=file]', function (e) {
        var $this = $(this);
        upLoadImg(e.currentTarget.files[0], function (obj) {
            var $dom = $this.siblings('[type=hidden]');
            var $old = $dom.val() ? $dom.val()+',' : '';
            var $new = $old+obj.save_path;
            $dom.val($new);

            var image = $this.siblings('.file-image');
            var index = image.children(".image-list:last-child").attr('data-index') ? parseInt(image.children(".image-list:last-child").attr('data-index'))+1 : 0;
            var html = '<div class="image-list" data-index="'+index+'" data-path="'+(index == 0 ? obj.save_path : ','+obj.save_path)+'">'+
                '<img src="'+obj.file_url+'" width=110 height="100">'+
                '<div class="delete">X</div>'+
                '</div>';
            image.append(html);
        });
    });

    $('.form-horizontal .file-images-add').on('click', '.file-image .delete', function (e) {
        var parent = $(this).parents('.image-list');
        var index = parent.attr('data-index');
        var path = parent.attr('data-path');
        var domVal = parent.parents('.file-image').siblings('[type=hidden]');
        console.log(path);console.log(domVal.val());
        var newDom = domVal.val().replace(path, '');
        console.log(newDom);
        domVal.val(newDom);
        parent.remove();
    });
</script>